package com.szitrus.smp.scalseal.util;

import ch.qos.logback.core.util.FileUtil;
import com.szitrus.smp.scalseal.exception.BusinessException;
import com.szitrus.smp.scalseal.exception.CommonErrorCodeEnum;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 描述：
 *
 * @author ssl
 * @create 2020/09/21 15:12
 */
public class ImageUtil {
    /**
     * 缩放图片
     *
     * @param image    图片Base64
     * @param toWidth  缩放后的宽
     * @param toHeight 缩放后的高
     * @return 返回缩放后的图片Base64
     * @throws BusinessException
     */
    public static String scale(String image, int toWidth, int toHeight) throws BusinessException {

        ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(image));
        BufferedImage bi;
        try {
            bi = ImageIO.read(bais);
        } catch (IOException e) {
            throw new BusinessException(CommonErrorCodeEnum.B_1002, e);
        }
        /* 新生成结果图片 */
        BufferedImage result = new BufferedImage(toWidth, toHeight,
                BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(bi.getScaledInstance(toWidth, toHeight,
                Image.SCALE_SMOOTH), 0, 0, Color.WHITE, null);
        result = transferAlpha(result);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(result, "png", baos);
        } catch (IOException e) {
            throw new BusinessException(CommonErrorCodeEnum.B_1003, e);
        }
        return Base64.encodeBase64String(baos.toByteArray());
    }

    /**
     * 去除图片白底
     *
     * @param bi
     * @return
     * @throws BusinessException
     */
    public static BufferedImage transferAlpha(BufferedImage bi) throws BusinessException {
        try {
            Image image = bi;
            ImageIcon imageIcon = new ImageIcon(image);
            BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),
                    BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
            g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
            int alpha = 0;
            for (int i = bufferedImage.getMinY(); i < bufferedImage.getHeight(); i++) {
                for (int j = bufferedImage.getMinX(); j < bufferedImage.getWidth(); j++) {
                    int rgb = bufferedImage.getRGB(j, i);
                    int R = (rgb & 0xff0000) >> 16;
                    int G = (rgb & 0xff00) >> 8;
                    int B = (rgb & 0xff);
                    if (((255 - R) < 30) && ((255 - G) < 30) && ((255 - B) < 30)) {
                        rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
                    }
                    bufferedImage.setRGB(j, i, rgb);
                }
            }

            g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
            return bufferedImage;
        } catch (Exception e) {
            throw new BusinessException(CommonErrorCodeEnum.B_1004, e);
        }

    }
}
